package org.jgs1904.chanmufeng;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * 枚举式单例2
 *
 * @author: 默苍璃
 * @date: 2024-07-01 16:05
 */
public enum EnumSingleObject2 {

    INSTANCE;

    private Object data;

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public static EnumSingleObject2 getInstance() {
        return INSTANCE;
    }

    public static void main(String[] args) {
        EnumSingleObject2 s1 = EnumSingleObject2.getInstance();
        s1.setData(new Object());
        EnumSingleObject2 s2 = null;

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("SeriableSingleton.obj");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(s1);
            oos.flush();
            oos.close();
            fos.close();

            FileInputStream fis = new FileInputStream("SeriableSingleton.obj");
            ObjectInputStream ois = new ObjectInputStream(fis);
            s2 = (EnumSingleObject2) ois.readObject();
            ois.close();
            fis.close();


            // 输出为true
            System.out.println(s1 == s2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}


